﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

using Hangjing.Model;
using Hangjing.SQLServerDAL;
using Newtonsoft.Json;
using Hangjing.Common;

/// <summary>
/// 
/// </summary>
public partial class AndroidAPI_Deliver_GetOrderDetailByOrderId : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Clear();

        string orderid = HjNetHelper.GetQueryString("orderid");

        Custorder dal = new Custorder();
        Foodlist orderfood_bll = new Foodlist();


        IList<CustorderInfo> list = new List<CustorderInfo>();

        list = dal.DeliverGetOrderList4GetModel(orderid);

        CustorderInfo info = new CustorderInfo();

        StringBuilder orderjson = new StringBuilder();

        if (list != null && list.Count > 0)
        {
            info = list[0];
        }

        if (info.TogoId == 0)
        {
            //收件地址
            info.TogoName = info.OrderAddress;
            info.TogoTel = info.shopauthuser;
            info.TogoAddress = info.OrderAddrEx;

            string agent = Request.UserAgent.ToLower();
            if (agent.IndexOf("iphone") > -1 || agent.IndexOf("ipad") > -1 || agent.IndexOf("ipod") > -1) //ios现在反了，这个先成接口换下
            {
                string arrive_name = info.OrderAddress;
                string arrive_tel = info.shopauthuser;
                string arrive_address = info.OrderAddrEx;

                info.TogoName = info.OrderRcver;
                info.TogoTel = info.OrderComm;
                info.TogoAddress = info.AddressText;


                info.OrderRcver = arrive_name;
                info.OrderComm = arrive_tel;
                info.AddressText = arrive_address;
            }


        }





        orderjson.Append("{\"totalmoney\":\"" + info.OrderSums.ToString("0.0") + "\",\"phone\":\"" + info.OrderComm + "\",\"SentTime\":\"" + info.SendTime + "\",\"ReachTime\":\"" + info.SendTime + "\",\"state\":\"" + info.OrderStatus.ToString() + "\",\"userid\":\"" + info.UserId.ToString() + "\",\"foodinorderString\":\"\",\"list\":[");

        IList<FoodlistInfo> food_list = orderfood_bll.GetAllByOrderID(orderid);

        foreach (FoodlistInfo finfo in food_list)
        {
            orderjson.Append("{\"count\":" + finfo.FCounts.ToString() + ",\"id\":\"" + finfo.FoodUnid.ToString() + "\",\"price\":" + finfo.FoodPrice.ToString("0.0") + ",\"name\":\"" + finfo.FoodName + "\"},");
        }
        orderjson.Append("],");

        string OrderAttach = info.OrderAttach;
        if (info.paystate == 1 && info.paymoney >0)
        {
            OrderAttach += "[已付"+info.paymoney+"元]";
        }

        orderjson.Append("\"shopname\":\"" + info.TogoName + "\",\"people\":1,\"addtime\":\"" + info.OrderDateTime.ToString("yyyy-MM-dd HH:mm:ss") + "\",\"username\":\"" + info.CustomerName + "\",");
        orderjson.Append("\"shopid\":\"" + info.TogoId.ToString() + "\",\"address\":\"" + info.AddressText + "\",\"Packagefree\":\"" + 0 + "\",\"sentmoney\":\"" + info.DeliverGetSendFee + "\",\"orderid\":\"" + info.orderid + "\",\"realname\":\"" + info.OrderRcver + "\",\"note\":\"" + info.OrderAttach + "\",\"shopaddress\":\"" + info.TogoAddress + "\",\"shoptel\":\"" + info.TogoTel + "\"");
        orderjson.Append(",\"PayMode\":\"" + info.paymode + "\"");
        orderjson.Append(",\"paymoney\":\"" + info.paymoney + "\"");
        orderjson.Append(",\"OrderTotal\":\"" + info.delivergetfee_base + "\"");
        orderjson.Append(",\"oldprice\":\"" + info.OldPrice.ToString() + "\"");

        orderjson.Append(",\"isupdate\":\"" + info.Sender + "\"");

        orderjson.Append(",\"SystemUserId\":\"" + info.SystemUserId + "\"");
        orderjson.Append(",\"cardids\":\"" + info.cardids + "\",");
        orderjson.Append("\"Commission\":\"" + 0 + "\",");
        orderjson.Append("\"settlecycle\":\"" + info.settlecycle + "\",");
        orderjson.Append("\"Advance\":\"" + info.Advance + "\",");
        orderjson.Append("\"Mergeorderremark\":\"" + info.Mergeorderremark + "\"");

        orderjson.Append(",\"sendstate\":\"" + info.sendstate + "\"");
        orderjson.Append(",\"IsShopSet\":\"" + info.IsShopSet + "\"");
        orderjson.Append(",\"paystate\":\"" + info.paystate.ToString() + "\"");
        orderjson.Append("," + info.ReveVar2.Replace("{", "").Replace("}", "").Replace("'", "\"") + "");  //ReveVar2的格式:{'ulat':'30.260804','ulng':'120.180593','slat':'30.270684','slng':'120.210628'}
        orderjson.Append("}");

        Response.Write(orderjson.ToString().Replace(",],", "],"));
        Response.End();
    }
}
